package com.liting.uitest.model;


import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 矩形
 */
@Data
@ApiModel(value = "矩形对象", description = "前端需要提交leftTopX、leftTopY、width、height、name；name自定义 ，同一个image中不同模块的name不能相同")
public class Rectangle extends Object {
    private String name;
    private double leftTopX;
    private double leftTopY;
    private double width;
    private double height;

    private Rectangle leftRect;
    private Rectangle rightRect;
    private Rectangle topRect;
    private Rectangle buttomRect;

    public BigDecimal getArea() {
        BigDecimal area = new BigDecimal(width).multiply(new BigDecimal(height)).setScale(2, RoundingMode.HALF_UP);
        return area;
    }

    public BigDecimal getProportion() {
        if (width > height) {
            return new BigDecimal(width).divide(new BigDecimal(height), BigDecimal.ROUND_HALF_UP).setScale(1, RoundingMode.UP);
        } else {
            return new BigDecimal(height).divide(new BigDecimal(width), BigDecimal.ROUND_HALF_UP).setScale(1, RoundingMode.UP);
        }
    }

    public boolean isInclude(int x, int y) {
        boolean xInclude = true, yInclude = true;
        if (x < leftTopX) {
            xInclude = false;
        } else if (x > leftTopX + width) {
            xInclude = false;
        } else {
            if (y < leftTopY) {
                yInclude = false;
            } else if (y > leftTopY + height) {
                yInclude = false;
            }
        }

        return xInclude && yInclude;
    }

    /**
     * 矩形中心点到输入点的距离
     *
     * @param centerX
     * @param centerY
     * @return
     */
    public double getDistance(double centerX, double centerY) {
        //1. 计算中心点
        double tempX = leftTopX + width / 2;
        double tempY = leftTopY + height / 2;

        BigDecimal xDistance = new BigDecimal(tempX).subtract(new BigDecimal(centerX)).abs();
        BigDecimal yDistance = new BigDecimal(tempY).subtract(new BigDecimal(centerY)).abs();

        BigDecimal temp = (xDistance.multiply(xDistance)).add(yDistance.multiply(yDistance));

        double distance = Math.sqrt(temp.doubleValue());

        return distance;

    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Rectangle) {
            Rectangle target = (Rectangle) obj;

            if ((target.getWidth() == this.getWidth()) && (target.getHeight() == this.getHeight())) {
                return true;
            } else {
                return false;
            }
        }

        return false;

    }

    public void splitFour(double centerX, double centerY) {
        double rightButtomX = leftTopX + width;
        double rightButtomY = leftTopY + height;

        if ((leftTopX < centerX) && (leftTopY < centerY)) {
            //左顶点在左上
            if ((rightButtomX < centerX) && (rightButtomY < centerY)) {
                //右底点在左上
                //这种情况，本身就是leftRect、topRect
                this.leftRect = new Rectangle();
                this.leftRect.setLeftTopY(this.getLeftTopY());
                this.leftRect.setLeftTopX(this.getLeftTopX());
                this.leftRect.setWidth(this.getWidth());
                this.leftRect.setHeight(this.getHeight());

                this.topRect = new Rectangle();
                this.topRect.setLeftTopY(this.getLeftTopY());
                this.topRect.setLeftTopX(this.getLeftTopX());
                this.topRect.setWidth(this.getWidth());
                this.topRect.setHeight(this.getHeight());

                this.rightRect = null;
                this.buttomRect = null;
            } else if ((rightButtomX < centerX) && (rightButtomY >= centerY)) {
                //右底点在左下
                //这种情况，本身就是leftRect; topRect,ButtomRect需要计算
                this.leftRect = new Rectangle();
                this.leftRect.setLeftTopY(this.getLeftTopY());
                this.leftRect.setLeftTopX(this.getLeftTopX());
                this.leftRect.setWidth(this.getWidth());
                this.leftRect.setHeight(this.getHeight());

                this.rightRect = null;

                this.topRect = new Rectangle();
                this.topRect.leftTopY = this.leftTopY;
                this.topRect.leftTopX = this.leftTopX;
                this.topRect.width = this.width;
                this.topRect.height = centerY - this.leftTopY;

                this.buttomRect = new Rectangle();
                this.buttomRect.leftTopY = centerY;
                this.buttomRect.leftTopX = this.leftTopX;
                this.buttomRect.width = this.width;
                this.buttomRect.height = rightButtomY - centerY;

            } else if ((rightButtomX >= centerX) && (rightButtomY < centerY)) {
                //右底点在右上
                //这种情况，本身就是topRect; leftRect,rightRect需要计算
                this.topRect = new Rectangle();
                this.topRect.setLeftTopY(this.getLeftTopY());
                this.topRect.setLeftTopX(this.getLeftTopX());
                this.topRect.setWidth(this.getWidth());
                this.topRect.setHeight(this.getHeight());

                this.buttomRect = null;

                this.leftRect = new Rectangle();
                this.leftRect.leftTopY = this.leftTopY;
                this.leftRect.leftTopX = this.leftTopX;
                this.leftRect.width = centerX - this.leftTopX;
                this.leftRect.height = this.height;

                this.rightRect = new Rectangle();
                this.rightRect.leftTopY = this.leftTopY;
                this.rightRect.leftTopX = centerX;
                this.rightRect.width = rightButtomX - centerX;
                this.rightRect.height = this.height;
            } else if ((rightButtomX >= centerX) && (rightButtomY >= centerY)) {
                //右底点在右下
                //这种情况，四个都需要计算
                this.topRect = new Rectangle();
                this.topRect.leftTopY = this.leftTopY;
                this.topRect.leftTopX = this.leftTopX;
                this.topRect.width = this.width;
                this.topRect.height = centerY - this.leftTopY;

                this.buttomRect = new Rectangle();
                this.buttomRect.leftTopY = centerY;
                this.buttomRect.leftTopX = this.leftTopX;
                this.buttomRect.width = this.width;
                this.buttomRect.height = rightButtomY - centerY;

                this.leftRect = new Rectangle();
                this.leftRect.leftTopY = this.leftTopY;
                this.leftRect.leftTopX = this.leftTopX;
                this.leftRect.width = centerX - this.leftTopX;
                this.leftRect.height = this.height;

                this.rightRect = new Rectangle();
                this.rightRect.leftTopY = this.leftTopY;
                this.rightRect.leftTopX = centerX;
                this.rightRect.width = rightButtomX - centerX;
                this.rightRect.height = this.height;
            }

        } else if ((leftTopX < centerX) && (leftTopY >= centerY)) {
            //左顶点在左下
            if ((rightButtomX < centerX) && (rightButtomY >= centerY)) {
                //右底点在左下
                //这种情况，本身就是leftRect; topRect=null,ButtomRect需要计算
                this.leftRect = new Rectangle();
                this.leftRect.setLeftTopY(this.getLeftTopY());
                this.leftRect.setLeftTopX(this.getLeftTopX());
                this.leftRect.setWidth(this.getWidth());
                this.leftRect.setHeight(this.getHeight());

                this.buttomRect = new Rectangle();
                this.buttomRect.setLeftTopY(this.getLeftTopY());
                this.buttomRect.setLeftTopX(this.getLeftTopX());
                this.buttomRect.setWidth(this.getWidth());
                this.buttomRect.setHeight(this.getHeight());

                this.rightRect = null;
                this.topRect = null;

            } else if ((rightButtomX >= centerX) && (rightButtomY >= centerY)) {
                //右底点在右下
                //这种情况，
                this.topRect = null;

                this.buttomRect = new Rectangle();
                this.buttomRect.setLeftTopY(this.getLeftTopY());
                this.buttomRect.setLeftTopX(this.getLeftTopX());
                this.buttomRect.setWidth(this.getWidth());
                this.buttomRect.setHeight(this.getHeight());

                this.leftRect = new Rectangle();
                this.leftRect.leftTopY = this.leftTopY;
                this.leftRect.leftTopX = this.leftTopX;
                this.leftRect.width = centerX - this.leftTopX;
                this.leftRect.height = this.height;

                this.rightRect = new Rectangle();
                this.rightRect.leftTopY = this.leftTopY;
                this.rightRect.leftTopX = centerX;
                this.rightRect.width = rightButtomX - centerX;
                this.rightRect.height = this.height;
            }


        } else if ((leftTopX >= centerX) && (leftTopY < centerY)) {
            //左顶点在右上
            if ((rightButtomX >= centerX) && (rightButtomY < centerY)) {
                //右底点在右上
                this.leftRect = null;
                this.buttomRect = null;
                this.rightRect = new Rectangle();
                this.rightRect.setLeftTopY(this.getLeftTopY());
                this.rightRect.setLeftTopX(this.getLeftTopX());
                this.rightRect.setWidth(this.getWidth());
                this.rightRect.setHeight(this.getHeight());

                this.topRect = new Rectangle();
                this.topRect.setLeftTopY(this.getLeftTopY());
                this.topRect.setLeftTopX(this.getLeftTopX());
                this.topRect.setWidth(this.getWidth());
                this.topRect.setHeight(this.getHeight());

            } else if ((rightButtomX >= centerX) && (rightButtomY >= centerY)) {
                //右底点在右下
                this.leftRect = null;

                this.rightRect = new Rectangle();
                this.rightRect.setLeftTopY(this.getLeftTopY());
                this.rightRect.setLeftTopX(this.getLeftTopX());
                this.rightRect.setWidth(this.getWidth());
                this.rightRect.setHeight(this.getHeight());

                this.topRect = new Rectangle();
                this.topRect.leftTopY = this.leftTopY;
                this.topRect.leftTopX = this.leftTopX;
                this.topRect.width = this.width;
                this.topRect.height = centerY - this.leftTopY;

                this.buttomRect = new Rectangle();
                this.buttomRect.leftTopY = centerY;
                this.buttomRect.leftTopX = this.leftTopX;
                this.buttomRect.width = this.width;
                this.buttomRect.height = rightButtomY - centerY;
            }

        } else if ((leftTopX >= centerX) && (leftTopY >= centerY)) {
            //左顶点在右下
            //此时，右底点肯定也在右下
            this.leftRect = null;
            this.buttomRect = new Rectangle();
            this.buttomRect.setLeftTopY(this.getLeftTopY());
            this.buttomRect.setLeftTopX(this.getLeftTopX());
            this.buttomRect.setWidth(this.getWidth());
            this.buttomRect.setHeight(this.getHeight());

            this.rightRect = new Rectangle();
            this.rightRect.setLeftTopY(this.getLeftTopY());
            this.rightRect.setLeftTopX(this.getLeftTopX());
            this.rightRect.setWidth(this.getWidth());
            this.rightRect.setHeight(this.getHeight());

            this.topRect = null;

        }
    }

}
